﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text;
using System.Data.SqlClient;

namespace EDMS
{
    public partial class SelectionInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }
        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton li = (LinkButton)sender;
            string id = li.CommandArgument;
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var result = dc.Xjdj.FirstOrDefault(x => x.xjbh == id);
                if (result != null)
                {
                    dc.Xjdj.DeleteOnSubmit(result);
                    dc.SubmitChanges();
                    Response.Write("<script>alert('删除成功！');</script>");
                    //location.href='SelectionInfo.aspx';
                }
            }
        }

        //判断选举编号是否重复
        private static bool checkXjbh(string bh)
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xjdj vo = dc.Xjdj.FirstOrDefault(v => v.xjbh == bh);
                if (vo != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        //确定添加
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtXjbh.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请添加选举编号！");
            }
            else if (txtXjbh.Text != null && checkXjbh(txtXjbh.Text) == false)
            {
                Frame.Global.GlobalFunctions.Alert("不允许录入重复的选举编号！");
            }
            else if (txtXjmc.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入选举名称！");
            }
            else if (teXjsj.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入选举时间！");
            }
            else if (teWtjssj.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入委托结束时间！");
            }
            else if (txtLxdh.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入联系电话！");
            }
            else
            {
                insertSelection();
            }
        }
        //添加选举信息
        private void insertSelection()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xjdj xj = new Xjdj();
                if (txtXjbh.Text != "" && checkXjbh(txtXjbh.Text) == true)
                {
                    xj.xjbh = txtXjbh.Text.Trim().ToString();
                }
                xj.xjmc = txtXjmc.Text.Trim().ToString();
                xj.xjsj = Convert.ToDateTime(teXjsj.Text);
                xj.wtjssj = Convert.ToDateTime(teWtjssj.Text);
                xj.flag = "A";
                xj.xjphone = txtLxdh.Text.Trim();
                xj.xjdz = meXjdz.Text.Trim();
                dc.Xjdj.InsertOnSubmit(xj);
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选举信息添加成功");
            }
        }
        //信息重置
        protected void btnEsc_Click(object sender, EventArgs e)
        {
            txtXjbh.Text = "";
            txtXjmc.Text = "";
            teXjsj.Text = "";
            teWtjssj.Text = "";
        }
        //条件查询
        protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string SqlFormat = @"select xjbh,xjmc,xjsj,wtjssj,xjphone,xjdz from xjdj where {0} 1=1";
            StringBuilder strb = new StringBuilder();
            List<SqlParameter> Params = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(cmbXjbh.Text.Trim()))
            {
                strb.Append("xjbh=@bh  AND ");
                Params.Add(new SqlParameter("@bh", cmbXjbh.Value.ToString()));
            }
            if (!string.IsNullOrEmpty(txtXjmcc.Text.Trim()))
            {
                strb.Append("xjmc like ''+@xm+'%'  AND ");
                Params.Add(new SqlParameter("@xm", txtXjmcc.Text.ToString()));
            }
            if (!string.IsNullOrEmpty(teXjsjc.Text))
            {
                strb.Append("xjsj = @sfz AND ");
                Params.Add(new SqlParameter("@sfz", teXjsjc.Text));
            }
            if (!string.IsNullOrEmpty(teWtjssjc.Text))
            {
                strb.Append("wtjssj =@sjh AND ");
                Params.Add(new SqlParameter("@sjh", teWtjssjc.Text));
            }
            this.gvSelection.DataSourceID = "";
            gvSelection.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
            gvSelection.DataBind();
        }

        protected void btnQk_Click(object sender, EventArgs e)
        {
            cmbXjbh.Text = "";
            txtXjmcc.Text = "";
            teXjsjc.Text = "";
            teWtjssjc.Text = "";
        }

        protected void btnXg_Click(object sender, EventArgs e)
        {
            updateSelection();
        }

        protected void btnQx_Click(object sender, EventArgs e)
        {

        }

        //更新数据
        private void updateSelection()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var se = dc.Xjdj.Select(s => s)
                    .Where(s => s.xjbh == txtXjbhx.Text.ToString())
                    .SingleOrDefault();
                se.xjmc = txtXjmcx.Text.Trim().ToString();
                se.xjsj = Convert.ToDateTime(teXjsjx.Text);
                se.wtjssj = Convert.ToDateTime(teWtjssjx.Text);
                se.xjphone = txtLxdhx.Text.Trim();
                se.xjdz = meXjdzx.Text.Trim();
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选举信息修改成功！");
            };

        }
    }
}